﻿using DAL.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.YQZS
{
    public class YqzsDAL
    {
        private readonly DataContext _dataContext;
        public YqzsDAL(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public async Task<IEnumerable<Yqzs>> ListAsync()
        {
            return await (from q in _dataContext.Yqzss
                          select q).ToListAsync();
        }

        public async Task<Yqzs> GetAsync(Guid id)
        {
            var data = await _dataContext.InOuts.Where(x => x.InOutId == id).Include("Yqzs").OrderByDescending(x => x.CreateTime).FirstOrDefaultAsync();
            return data?.Yqzs;
        }

        public async Task CreateAsync(Yqzs entity)
        {
            _dataContext.Entry(entity).State = EntityState.Added;
        }

        public async Task UpdateAsync(Yqzs entity)
        {
            _dataContext.Entry(entity).State = EntityState.Modified;
        }

        public async Task DeleteAsync(Guid id)
        {
            var entity = await GetAsync(id);
            _dataContext.Entry(entity).State = EntityState.Deleted;
        }

        public async Task<int> Count1Async(string deptid, string employeeType)
        {
            var query = from q in _dataContext.Employees.Include("InOuts").Include("InOuts.Yqzs")
                        select q;
            if (!string.IsNullOrEmpty(deptid)) query = query.Where(x => x.DepartmentId == deptid);
            if (!string.IsNullOrEmpty(employeeType)) query = query.Where(x => x.EmployeeType == employeeType);
            return await query.CountAsync(x => x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault() != null && x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault().InOutType == "进场" && !string.IsNullOrEmpty(x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault().Yqzs.Symptom) && !string.IsNullOrEmpty(x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault().Yqzs.SurveyResult));
        }
        public async Task<int> Count2Async(string deptid, string employeeType)
        {
            var query = from q in _dataContext.Employees.Include("Yqzs")
                        select q;
            if (!string.IsNullOrEmpty(deptid)) query = query.Where(x => x.DepartmentId == deptid);
            if (!string.IsNullOrEmpty(employeeType)) query = query.Where(x => x.EmployeeType == employeeType);
            return await query.CountAsync(x => x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault() == null || x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault().InOutType != "进场" || (string.IsNullOrEmpty(x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault().Yqzs.Symptom) && string.IsNullOrEmpty(x.InOuts.OrderByDescending(y => y.CreateTime).FirstOrDefault().Yqzs.SurveyResult)));
        }

        public async Task SaveChangesAsync()
        {
            await _dataContext.SaveChangesAsync();
        }
    }
}
